package com.hctg.forced2sleep.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.actor.myandroidframework.bean.OnActivityCallback;
import com.actor.myandroidframework.utils.ConfigUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hctg.forced2sleep.R;
import com.hctg.forced2sleep.activity.AppChooseActivity;
import com.hctg.forced2sleep.activity.AppMonitorSetActivity;
import com.hctg.forced2sleep.activity.BaseActivity;
import com.hctg.forced2sleep.bean.MineItem;
import com.hctg.forced2sleep.dialog.Tips2BtnDialog;
import com.hctg.forced2sleep.widget.ProgressBar;

import java.util.Calendar;
import java.util.Collection;
import java.util.List;

public class AppMonitorGridAdapter extends BaseQuickAdapter<MineItem, BaseViewHolder> {
//    public ExecutorService A;
    public PackageManager B;
    public int C;

    public AppMonitorGridAdapter() {
        super(R.layout.app_monitor_item);
//        this.A = Executors.newSingleThreadExecutor();
        this.B = ConfigUtils.APPLICATION.getPackageManager();
        this.C = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);

        setOnItemClickListener((adapter, view, position) -> {
            //if是最后1个item
            if (position == getData().size() - 1) {
                //跳转选择app
                Context context = getContext();
                if (context instanceof BaseActivity) {
                    AppChooseActivity.start((BaseActivity<?>) context, (resultCode, data) -> {
                        if (resultCode == Activity.RESULT_OK && data != null) {
                            String json = data.getStringExtra(AppChooseActivity.RESULT_KEY);
                            MineItem bean = GsonUtils.fromJson(json, MineItem.class);
                            if (bean != null) {
                                addData(bean);
                                //跳转编辑
                                OnItemClickListener itemClickListener = getOnItemClickListener();
                                if (itemClickListener != null) {
                                    itemClickListener.onItemClick(adapter, view, position);
                                }
                            }
                        }
                    });
                }
            } else {
                //跳转编辑
                Context context = getContext();
                if (context instanceof BaseActivity) {
                    MineItem item = getItem(position);
                    AppMonitorSetActivity.start((BaseActivity<?>) context, item, (resultCode, data) -> {
                        if (resultCode == Activity.RESULT_OK && data != null) {
                            String json = data.getStringExtra(AppMonitorSetActivity.RESULT_KEY);
                            MineItem bean = GsonUtils.fromJson(json, MineItem.class);
                            if (bean != null) {
                                //更新数据
                                getData().set(position, bean);
                                notifyItemChanged(position);
                            }
                        }
                    });
                }
            }
        });

        //长按删除
        setOnItemLongClickListener((adapter, view, position) -> {
            MineItem item = getItem(position);
            new Tips2BtnDialog(getContext(), "删除", "确认删除\"" + item.name + "\"吗?", v -> {
                removeAt(position);
            }).show();
            return false;
        });

        addData(new MineItem(0, R.drawable.icon_add_gray_round, ""));
    }

    @Override
    public void convert(@NonNull BaseViewHolder holder, MineItem item) {
        int position = holder.getAbsoluteAdapterPosition();
        //if是最后1个item
        if (position == getData().size() - 1) {
            holder.setImageResource(R.id.riv_img, item.resId)
                    .setText(R.id.tv_app_name, "")
                    .setText(R.id.tv_limit_time, "")
                    .setVisible(R.id.view_app_front, false)
                    .setVisible(R.id.iv_lock, false)
                    .setVisible(R.id.progress_bar, false)
            ;
        } else {
            holder.setImageDrawable(R.id.riv_img, AppUtils.getAppIcon(item.subName))
                    .setText(R.id.tv_app_name, item.name)
                    .setText(R.id.tv_limit_time, "12小时")
                    .setVisible(R.id.view_app_front, position % 2 == 1)
                    .setVisible(R.id.iv_lock, position % 2 == 1)
                    .setVisible(R.id.progress_bar, true)
            ;
            ProgressBar progressBar = holder.getView(R.id.progress_bar);
            progressBar.setText("剩余11小时58分钟");
            progressBar.setMax(12 * 60);
            progressBar.setProgress(2);
        }
    }

    @Override
    public void addData(@NonNull MineItem data) {
        addData(Math.max(getData().size() - 1, 0), data);
    }

    @Override
    public void setList(@Nullable Collection<? extends MineItem> list) {
        super.setList(list);
        addData(new MineItem(0, R.drawable.icon_add_gray_round, ""));
    }
}
